/*
* $Id: TypeTable.cpp 164 2010-10-10 21:19:58Z volok-aleksej@yandex.ru $
*
* Copyright 2010 by Volokitin Alexey
* All rights reserved
*
*/
#include "Common/include.h"
#include "TypeTable.h"
#include "../SqlBinder.h"

CTypeTable::CTypeTable(const std::tstring& strName, int nDefTypes)
: CTable(strName), m_nDefaultTypes(nDefTypes){}
CTypeTable::~CTypeTable(){}

void CTypeTable::CreateTable(IDatabase* pDatabase){
	if(!pDatabase->IsExistTable(GetNameTable())){
		std::tstring strRequest(_T("CREATE TABLE IF NOT EXISTS "));
		pDatabase->ExecRequest(strRequest + GetNameTable() + 
			_T("(id_type INTEGER NULL,")\
			_T("name TEXT NOT NULL,")\
			_T("PRIMARY KEY(id_type ASC AUTOINCREMENT));"));
		for(int i = 0; i < m_nDefaultTypes; i++){
			m_content.s_nId = i;
			SetValue(pDatabase);
		}
	}
}
void CTypeTable::UpdateValue(IDatabase* pDatabase){
	std::tstring strRequest(_T("UPDATE "));
	CSqlBinder binder(strRequest + GetNameTable() + 
		_T(" SET name=? WHERE id_type=?"));
	binder.SetValue(1, m_content.s_nId);
	binder.SetValue(0, m_content.s_strName);
	pDatabase->ExecRequest(binder.GetRequest());
	m_content.Clear();
}
IResult* CTypeTable::GetValue(IDatabase* pDatabase){
	CSqlBinder binder;
	std::tstring strRequest(_T("SELECT * FROM "));
	strRequest = strRequest + GetNameTable();
	if(m_content.s_nId){
		binder.SetRequest(strRequest + _T(" WHERE id_type=?"));
		binder.SetValue(0, m_content.s_nId);
	}else
		binder.SetRequest(strRequest);
	m_content.Clear();
	return FillContent(pDatabase->ExecRequest(binder.GetRequest()));
}
void CTypeTable::SetValue(IDatabase* pDatabase){
	std::tstring strRequest = _T("SELECT MAX(id_type) as id_type FROM ");
	MyTable table = pDatabase->ExecRequest(strRequest + GetNameTable());
	if(table.empty() || !table.empty() && table[0][0].m_strValueColumn.empty())
		m_content.s_nId = 0;
	else
		m_content.s_nId = _ttoi(table[0][0].m_strValueColumn.c_str()) + 1;
	CSqlBinder binder;
	strRequest = _T("INSERT INTO ");
	binder.SetRequest(strRequest + GetNameTable() + 
		_T("(id_type, name) VALUES(?, ?)"));
	binder.SetValue(0, m_content.s_nId);
	binder.SetValue(1, m_content.s_strName);
	pDatabase->ExecRequest(binder.GetRequest());
}
void CTypeTable::DeleteValue(IDatabase* pDatabase){
	CSqlBinder binder;
	std::tstring strRequest(_T("DELETE FROM "));
	if(m_content.s_nId){
		CSqlBinder binder(strRequest + GetNameTable() + 
			_T(" WHERE id_type=?"));
		binder.SetValue(0, m_content.s_nId);
		pDatabase->ExecRequest(binder.GetRequest());
	}
	m_content.Clear();
}
IOperation::i_content* CTypeTable::CreateContent(){
	return new content;
}

CTypeTable::content::content(){
	Clear();
}
void CTypeTable::content::Clear(){
	s_nId = 0;
	s_strName.clear();
}
void CTypeTable::content::Set(const std::vector<CTableItem>& aContent){
	for(std::vector<CTableItem>::const_iterator it = aContent.begin(); it != aContent.end(); it++){
		if(it->m_strNameColumn == _T("id_type"))
			s_nId = _ttoi(it->m_strValueColumn.c_str());
		else if(it->m_strNameColumn == _T("name"))
			s_strName = it->m_strValueColumn;
	}
}
void CTypeTable::content::Free(){
	delete this;
}
